from django.shortcuts import render,get_object_or_404
from .models import Product,Category
from cart.forms import CartAddProductForm

from django.views.generic.list import ListView
from django.views.generic.edit import CreateView, UpdateView,DeleteView
# Create your views here.

class OwnerMixin(object):
	def get_queryset(self):
		qs = super(OwnerMixin,self).get_queryset()
		return qs.filter(owner=self.request.user)

class OwnerEditMixin(object):
	def form_valid(self,form):
		form.instance.owner = self.request.user
		return super(OwnerEditMixin,self).form_valid(form)

class OwnerCourseMixin(OwnerMixin):
	model = Product

class OwnerCourseEditMixin(OwnerCourseMixin,OwnerEditMixin):
	fields = [],
	
	template_name = ''

def product_list(request,category_slug=None):
	category = None
	categories = Category.objects.all()
	products = Product.objects.filter(available=True)
	if category_slug:
		category = get_object_or_404(Category,slug=category_slug)
		products = products.filter(category = category)
	return render(request,'shop/product/list.html',{
		'category':category,
		'categories' : categories,
		'products': products
		})

def product_detail(request,id,slug):
	product = get_object_or_404(Product,id=id,slug=slug,available=True)
	cart_product_form = CartAddProductForm()
	return render(request,'shop/product/detail.html',{'product':product,'cart_product_form':cart_product_form})